/**
 * 
 */
package com.yitong.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import com.yitong.bean.UserInfo;

/**
 * @ClassName: SessionUtils
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2017年7月23日 下午1:01:33
 */
public abstract class SessionUtils {

	public static final String ATTR_NAME_AUTHC_TOKEN = "AuthcToken";

	public static final String ATTR_NAME_USER = "USER_INFO";

	public static Session getSession(boolean create) {
		return SecurityUtils.getSubject().getSession(create);
	}

	public static boolean setAttribute(Object name, Object value) {
		Session session = getSession(false);
		if (session != null) {
			session.setAttribute(name, value);
			return true;
		}
		return false;
	}

	public static Object removeAttribute(Object name) {
		Session session = getSession(false);
		if (session != null) {
			return session.removeAttribute(name);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getAttribute(Object name) {
		Session session = getSession(false);
		if (session != null) {
			return (T) session.getAttribute(name);
		}
		return null;
	}

	public static boolean setAuthcToken(String token) {
		return setAttribute(ATTR_NAME_AUTHC_TOKEN, token);
	}

	public static Object removeAuthcToken() {
		return removeAttribute(ATTR_NAME_AUTHC_TOKEN);
	}

	public static String getAuthcToken() {
		return getAttribute(ATTR_NAME_AUTHC_TOKEN);
	}

	public static boolean setUser(UserInfo user) {
		return setAttribute(ATTR_NAME_USER, user);
	}

	public static UserInfo getUserInfo() {
		return getAttribute(ATTR_NAME_USER);
	}
}
